package top.pengdong.pictureShare.userAuth.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import top.pengdong.pictureShare.common.model.LoginVal;
import top.pengdong.pictureShare.common.model.R;
import top.pengdong.pictureShare.common.model.ResultMsg;
import top.pengdong.pictureShare.common.model.SysConstant;
import top.pengdong.pictureShare.common.utils.OauthUtils;
import top.pengdong.pictureShare.userAuth.feign.mail.MailFeignServer;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/oauth")
@Slf4j
public class AuthController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @PostMapping("/logout")
    public ResultMsg logout(){
        LoginVal loginVal = OauthUtils.getCurrentUser();
        log.info("令牌唯一ID：{},过期时间：{}",loginVal.getJti(),loginVal.getExpireIn());
        //这个jti放入redis中，并且过期时间设置为token的过期时间
        stringRedisTemplate.opsForValue().set(SysConstant.JTI_KEY_PREFIX+loginVal.getJti(),"",loginVal.getExpireIn(), TimeUnit.SECONDS);
        return new ResultMsg(200,"注销成功",null);
    }

    @GetMapping("/check_token")
    public ResultMsg checkToken() {
        LoginVal loginVal = OauthUtils.getCurrentUser();
        if (loginVal != null)
            return new ResultMsg(200, "验证成功", null);
        else {
            return new ResultMsg(400, "验证失败", null);
        }
    }


    @GetMapping("/test")
    public String test(){
        return "oauth 协议能够启动";
    }
}